package com.base.system.managedBean;

import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.user.util.Browser;

import com.base.organization.entity.DepartmentalUnit;
import com.base.security.SecurityConst;
import com.base.system.managedBean.baseBean.BaseSecurityDepartmentBean;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_showDepartmentalUnitBean", scope = ManagedBeanScope.REQUEST)
public class ShowDepartmentalUnitBean extends BaseSecurityDepartmentBean {
	private final static Log logger = LogFactory
			.getLog(ShowDepartmentalUnitBean.class);
	@Inject
	FacesContext context;

	protected void initSpecialProperty() {
		Map<String, Object> session = context.getExternalContext()
				.getSessionMap();
		departmentalUnit = (DepartmentalUnit) session
				.get(SecurityConst.DEPARTMENTALUNIT_SELECT_ROW_DATA);
		this.getDepartmentProperty();
	}

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		// System.out.println("show 中的 postBack:" + isPostback);
		// 只有第一加载的时候才从session进行取值，后来提交后就不取值了
		if (!isPostback) {
			this.initSpecialProperty();
		}
	}

	@Action(id = "modifyDepartmentUnit")
	public void modifyDepartmentUnit() {
		String tabUrl = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestContextPath()
				+ "/modules/system/editDepartmentalUnit.faces?departmentId="
				+ departmentalUnitId;
		String tabId = SecurityConst.DEPARTMENTALUNIT_EDIT_TAB;
		String tabTitle = "编辑部门";
		String script = "addCenterLayoutTab('" + tabId + "', '" + tabTitle
				+ "', '" + tabUrl + "');";
		Browser.execClientScript(script);

		Browser.execClientScript("closeCenterLayoutTab('"
				+ SecurityConst.DEPARTMENTALUNIT_SHOW_TAB + "');");
	}

	@Action(id = "createDepartment")
	public void createDepartment() {
		String tabUrl = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestContextPath()
				+ "/modules/system/addDepartmentalUnit.faces?parentDepartmentalUnitId="
				+ departmentalUnitId;
		String tabId = SecurityConst.DEPARTMENTALUNIT_ADD_TAB;
		String tabTitle = "新增下级部门";
		String script = "addCenterLayoutTab('" + tabId + "', '" + tabTitle
				+ "', '" + tabUrl + "');";
		Browser.execClientScript(script);

		Browser.execClientScript("closeCenterLayoutTab('"
				+ SecurityConst.DEPARTMENTALUNIT_SHOW_TAB + "');");
	}

}
